package com.yunhe.policyfulfillment.policyful.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfoApplyMatter;
import com.yunhe.policyfulfillment.policyful.model.*;
import com.yunhe.policyfulfillment.policyful.service.PolicyInfoApplyMatterService;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.yunhe.common.model.ResultObject;

import java.util.List;

/**
 * @description 政策兑现企业在线申请/审批事项
 * @author hetao
 * @date 2021-08-24
 */
@Api(value = "PolicyInfoApplyMatter", description = "政策兑现企业在线申请/审批事项维护", tags = {"Model_PolicyInfoApplyMatter"})
@RestController
@RequestMapping(value = "/policyInfoApplyMatter")
public class PolicyInfoApplyMatterController {

    @Autowired
    private PolicyInfoApplyMatterService policyInfoApplyMatterService;

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    /**
     * 新增
     * @author hetao
     * @date 2021/08/24
     **/
    @ApiOperation("企业在线申请事项")
    @PostMapping("/createPolicyInfoApplyMatter")
    public ResponseEntity<ResultObject> createPolicyInfoApplyMatter(@RequestBody PolicyInfoApplyMatterBo policyInfoApplyMatterBo) throws ArgumentErrorException {
        policyInfoApplyMatterService.createPolicyInfoApplyMatter(policyInfoApplyMatterBo);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 申请审批分组列表查询
     * @param queryCondition
     * @return 分页信息
     * @author 张翔宇
     * @date 2021年11月17日 11点07分
     */
    @ApiOperation("申请审批分组列表查询")
    @GetMapping("/findApprovalGroupInfo")
    public  ResponseEntity<ResultObject<List<PolicyInfoApplyMatterVo>>> findApprovalGroupInfo(PolicyInfoApplyMatterQueryCondition queryCondition) {
        return new ResponseEntity(new ResultObject<>(policyInfoApplyMatterService.findApprovalGroupInfo(queryCondition)), HttpStatus.OK);
    }

    @ApiOperation("申请审批列表查询")
    @PostMapping("/findPolicyInfoApplyMatterByQueryCondition")
    public ResponseEntity<ResultObject<List<PolicyInfoApplyMatterVo>>> findPolicyInfoApplyMatterByQueryCondition(
            @RequestBody PolicyInfoApplyMatterQueryCondition policyInfoApplyMatterQueryCondition) throws ArgumentErrorException {
        PageResult pageResult  = policyInfoApplyMatterService.findPolicyInfoApplyMatterByQueryCondition(policyInfoApplyMatterQueryCondition);
        return new ResponseEntity(new ResultObject<>(pageResult), HttpStatus.OK);
    }

    /**
     * 历史分组信息查询
     * @param queryCondition
     * @return
     * @author 张翔宇
     * @date 2021年11月17日14点06分
     */
    @ApiOperation("申请审批分组列表查询")
    @GetMapping("/findApprovalHistoryGroupInfo")
    public  ResponseEntity<ResultObject<List<PolicyInfoApplyMatterVo>>> findApprovalHistoryGroupInfo(PolicyInfoApplyMatterQueryCondition queryCondition) {
        return new ResponseEntity(new ResultObject<>(policyInfoApplyMatterService.findApprovalHistoryGroupInfo(queryCondition)), HttpStatus.OK);
    }

    /**
     * 审批列表历史记录查询
     * 此处包含结束得审批与已过期得审批
     * @return 包装类
     * @author 张翔宇
     * @date 2021年11月15日 15点11分
     */
    @ApiOperation("审批列表历史记录")
    @GetMapping("/findApprovalHistory")
    public ResponseEntity<ResultObject<List<PolicyAuditProcessVo>>> findApprovalHistory(
            PolicyInfoApplyMatterQueryCondition queryCondition
    ) {
        return new ResponseEntity(new ResultObject<>(policyInfoApplyMatterService.findApprovalHistory(
                queryCondition
        )), HttpStatus.OK);
    }

    /**
     * 刪除
     * @author hetao
     * @date 2021/08/24
     **/
    @ApiOperation("根据id删除政策兑现企业在线申请/审批事项")
    @DeleteMapping("/deletePolicyInfoApplyMatter")
    public ResponseEntity<ResultObject> deletePolicyInfoApplyMatter(@RequestParam(value = "id",required = true) int id) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.Admin.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是管理员不能进行删除！");
        }
        policyInfoApplyMatterService.deletePolicyInfoApplyMatter(id);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 催办
     * @author hetao
     * @date 2021/08/24
     **/
    @ApiOperation("催办")
    @PostMapping("/urgeApplyMatter")
    public ResponseEntity<ResultObject> urgeApplyMatter(@RequestParam(value = "id",required = true) int id) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.PolicyEnforcementOfficeAudit.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是政策兑现办的用户不能进行催办！");
        }
        policyInfoApplyMatterService.urgeApplyMatter(id);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 通过id 查询审批流程
     * @author hetao
     * @date 2021/08/24
     **/
    @ApiOperation("通过id 查询审批流程")
    @PostMapping("/findAuditProcessByApplyMatterId")
    public ResponseEntity<ResultObject<List<PolicyAuditProcessVo>>> findAuditProcessByApplyMatterId(@RequestParam(value = "id",required = true) int id)  {
        List<PolicyAuditProcessVo> result = policyInfoApplyMatterService.findAuditProcessByApplyMatterId(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 审核
     * @author hetao
     * @date 2021/08/24
     **/
    @ApiOperation("审核")
    @PostMapping("/auditMatter")
    public ResponseEntity<ResultObject> auditMatter(@RequestBody AuditMatterBo auditMatterBo) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.PolicyEnforcementOfficeAudit.value().equals(headerUser.getRoleName()) &&
                !PolicyFulfillmentConstant.Role_Name.Park.value().equals(headerUser.getRoleName()) &&
                !PolicyFulfillmentConstant.Role_Name.Financial.value().equals(headerUser.getRoleName()) &&
            !PolicyFulfillmentConstant.Role_Name.SupervisorOrg.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是主管部门/政策办/园区/财政部门用户不能进行审核操作！");
        }
        policyInfoApplyMatterService.auditMatter(auditMatterBo);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("批量审核")
    @PostMapping("/batchAuditMatter")
    public ResponseEntity<ResultObject> batchAuditMatter(@RequestBody AuditMatterBo auditMatterBo) throws ArgumentErrorException {
        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(!PolicyFulfillmentConstant.Role_Name.PolicyEnforcementOfficeAudit.value().equals(headerUser.getRoleName()) &&
                !PolicyFulfillmentConstant.Role_Name.Park.value().equals(headerUser.getRoleName()) &&
                !PolicyFulfillmentConstant.Role_Name.Financial.value().equals(headerUser.getRoleName()) &&
                !PolicyFulfillmentConstant.Role_Name.SupervisorOrg.value().equals(headerUser.getRoleName())){
            throw new ArgumentErrorException("当前登录用户不是主管部门/政策办/园区/财政部门用户不能进行审核操作！");
        }
        policyInfoApplyMatterService.auditMatter(auditMatterBo);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 查询 根据主键 id 查询
     * @author hetao
     * @date 2021/08/24
     **/
    @ApiOperation("根据id查询政策兑现企业在线申请/审批事项")
    @GetMapping("/findPolicyInfoApplyMatterById")
    public ResponseEntity<ResultObject<PolicyInfoApplyMatterDetailVo>> findPolicyInfoApplyMatterById(@RequestParam(value = "id",required = true) int id){
        return new ResponseEntity<>(new ResultObject(policyInfoApplyMatterService.findPolicyInfoApplyMatterById(id)),HttpStatus.OK);
    }


}
